package utils;

/**
 * 图类
 * @author 车宗绍Che Zongshao
 * @date 2015/1/7
 */

public class Graph {

	private static Node root;			// 起点
	private static Node target;			// 终点
	private static Node[] nodes;		// 其他节点, 包括终点
	private static Edge[] edges;		// 边
	
	/**
	 * 图的构造函数，构造了节点的邻接边
	 * @param root
	 * @param target
	 * @param nodes
	 * @param edges
	 */
	public Graph(Node root, Node target, Node[] nodes, Edge[] edges) {
		Graph.root = root;
		Graph.target = target;
		Graph.nodes = nodes;
		Graph.edges = edges;
		// 构造邻接边
		for (int j = 0; j < nodes.length; j++) {
			Edge[] nextEdges;
			int k = 0;
			if (nodes[j].getName() <= nodes.length) {
				// B:2 C:3 D:4
				nextEdges = new Edge[nodes.length];
				for (int i = 0; i < edges.length; i++) {
					int startName = edges[i].getStart().getName();
					if (startName == nodes[j].getName()) {
						nextEdges[k] = edges[i];
						k++;
					}
				}
				nodes[j].setNextEdges(nextEdges);
			} 
		}
	}
	
	/**
	 * 用name生成Node
	 * @param name
	 * @return
	 */
	public static Node generateNode(int name) {
		if (name == 1) {
			// root
			Node newNode = new Node();
			newNode.setName(name);
			newNode.setNextEdges(root.getNextEdges());
			return newNode;
		}
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].getName() == name) {
				Node newNode = new Node();
				newNode.setName(name);
				newNode.setNextEdges(nodes[i].getNextEdges());
				return newNode;
			}
		}
		return null;
	}
	
	public static Node[] getNodes() {
		return nodes;
	}
	public static Node getRoot() {
		return root;
	}
	public static void setRoot(Node root) {
		Graph.root = root;
	}
	public static Node getTarget() {
		return target;
	}
	public static void setTarget(Node target) {
		Graph.target = target;
	}
	public static void setNodes(Node[] nodes) {
		Graph.nodes = nodes;
	}
	public static Edge[] getEdges() {
		return edges;
	}
	public static void setEdges(Edge[] edges) {
		Graph.edges = edges;
	}
}